tree grammar GrammarEvaluator;

options {
  language = Java;
  tokenVocab = BasicArithmeticEqu;
  ASTLabelType = CommonTree;
}

@header{
	package calc;
	import java.util.Map;
	import java.util.HashMap;
	import java.util.Random;
	import calc.equations.*;
}

@members {
	public Map<String, Integer> variables = new HashMap<String, Integer>();
	BuildEquation build = new BuildEquation();
}

problem returns [Equation eq]
	: op = operation {eq = op;}
	;

operation returns [Equation eq]
	:	ADD {eq = build.expandEquation(null, 0, 3);}
	|	SUB {eq = build.expandEquation(null, 1, 1);}
	|	MULT {eq = build.expandEquation(null, 2, 2);}
	;

evaluator returns [int result]
	: e=expression EOF {result = e;}
	;

expression returns [int result]
	:	^('+' op1=expression op2=expression) {result = op1 + op2;}
	|	^('-' op1=expression op2=expression) {result = op1 - op2;}
	|	^('*' op1=expression op2=expression) {result = op1 * op2;}
	|	^('/' op1=expression op2=expression){
				if(op2 == 0){
					result = 100*(op2 / op1);
				}else{
					result = 100*(op1 / op2);
				}
			}
	|	^('mod' op1=expression op2=expression) {result = op1 \% op2;}
	|	^(NEGATION e=expression) {result = -e;}
	| IDENT {
			if(variables.containsKey($IDENT.text)){
				result = variables.get($IDENT.text);
			}else{
				Random randomGenerator = new Random();
				result = randomGenerator.nextInt(10);
				variables.put($IDENT.text, result);
			}
		}
	| INTEGER {result = Integer.parseInt($INTEGER.text);}
	| VAR {
			
		}
	;